package com.gztms.blogmgnt.controller;

import com.gztms.blogmgnt.pojo.Blog;
import com.gztms.blogmgnt.pojo.BlogStatus;
import com.gztms.blogmgnt.pojo.vo.BlogVO;
import com.gztms.blogmgnt.service.BlogService;
import com.gztms.enums.AppCodeEnum;
import com.gztms.utils.BeanCopyUtils;
import com.gztms.utils.ResultUtil;
import com.gztms.utils.SecurityUtils;
import com.gztms.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import java.io.IOException;

/**
 * @author Zoey
 * @date 2023/4/23 0:23
 * @Description TODO
 **/
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 发布文章
     * @param blog
     * @return
     */
    @PostMapping("/addBlog")
    public ResultUtil addBlog(@RequestBody Blog blog) {
        return blogService.addBlog(blog);
    }

    /**
     * 查看文章详情
     * @param id
     * @return
     */
    @PermitAll
    @GetMapping("/{id}")
    public ResultUtil getBlogDetail(@PathVariable("id") Integer id) {
        return blogService.getBlogDetail(id);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @GetMapping("/delBlog/{id}")
    public ResultUtil delBlog(@PathVariable( "id") Integer id) {
        return blogService.delBlog(id);
    }

    /**
     * 修改文章
     * @param blog
     * @return
     */
    @PostMapping("/updateBlog")
    public ResultUtil updateBlog(@RequestBody Blog blog) {
//        Blog blog = BeanCopyUtils.copyBean(blogVO, Blog.class);
        return blogService.updateBlog(blog);
    }

    /**
     * 查询所有文章
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getAllBlogs")
    public ResultUtil getAllBlogs(
            @RequestParam(value = "pageNum") int pageNum,
            @RequestParam(value = "pageSize") int pageSize) {
        return blogService.getAllBlogs(pageNum,pageSize);
    }

    /**
     * 查询全部文章类别
     * @return
     */
    @GetMapping("/getAllTypes")
    public ResultUtil getAllTypes() {
        return blogService.getAllTypes();
    }

    @PostMapping("/check/{id}")
    public  ResultUtil checkBlogById(){
        return null;
    }

    /**
     * 获取热门文章
     * @return
     */
    @GetMapping("/getHotBlogs")
//    @Cacheable(value = "getHotBlogs", key = "targetClass + methodName")
    public ResultUtil getHotBlogs() {
        return blogService.getHotBlogs();
    }

    /**
     * 按条件查询全部文章列表(用户)
     * @param pageNum
     * @param pageSize
     * @param type
     * @param title
     * @param isUp 时间顺序
     * @return
     */
    @GetMapping("/getBlogList")
    public ResultUtil getBlogList(
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "isUp", defaultValue = BlogStatus.BLOG_DESC_BY_TIME, required = false) Integer isUp
    ) {
        return blogService.getBlogList(pageNum,pageSize,type,title,isUp);
    }

    /**
     * 更新文章浏览量
     * @param id
     * @return
     */
    @PutMapping("/updateViewCount/{id}")
    public ResultUtil updateViewCount(@PathVariable Integer id) {
        return blogService.updateViewCount(id);
    }

    @PostMapping("/uploadImg")
    public ResultUtil uploadImg(@RequestParam(value = "file") MultipartFile file) throws IOException {
        String imgUrl = UploadUtil.uploadImage(file);
        return blogService.uploadImage(imgUrl);
    }

    @GetMapping("/getBlogsByUid")
    public ResultUtil getBLogsByUid(
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        String uid = SecurityUtils.getUid();
        if (uid == null) {
            return ResultUtil.errorResult(AppCodeEnum.SYSTEM_ERROR);
        }

        return blogService.getBlogByUid(pageNum,pageSize,status,uid);
    }



}
